class Solution
{
public:
    int rotatedDigits(int n)
    {
        int goodCount = 0;
        for (int i = 1; i <= n; ++i)
        {
            bool contain2569 = false;
            bool contain347 = false;
            int num = i;
            while (num)
            {
                int numLow = num % 10;
                if (numLow == 3 || numLow == 4 || numLow == 7)
                {
                    contain347 = true;
                    break;
                }
                else if (numLow == 2 || numLow == 5 || numLow == 6 || numLow == 9)
                {
                    contain2569 = true;
                }
                num /= 10;
            }
            if (contain2569 && !contain347)
            {
                ++goodCount;
            }
        }
        return goodCount;
    }
};